/*
 * > /*
 >  * INSANE - Interactive Structural Analysis Environment
 >  *
 >  * Copyright (C) 2003-2004
 >  * Universidade Federal de Minas Gerais
 >  * Escola de Engenharia
 >  * Departamento de Engenharia de Estruturas
 >  *
 >  * Author's email :     insane@dees.ufmg.br
 >  * Author's Website :   http://www.dees.ufmg.br/insane
 >  *
 >  * This program is free software; you can redistribute it and/or
 >  * modify it under the terms of the GNU General Public License
 >  * as published by the Free Software Foundation; either version 2
 >  * of the License, or any later version.
 >  *
 >  * This program is distributed in the hope that it will be useful,
 >  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 >  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 >  * GNU General Public License for more details.
 >  *
 >  * You should have received a copy of the GNU General Public License
 >  * along with this program; if not, write to the Free Software
 >  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 > 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.command;

import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.persistence.PostpPersistence.PostpPersistenceAsXML;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;

/**
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since January 2004
 */
public class AnimationStartStepCommand implements Command {
	
	private InterfacePostp parent;
	private Thread thread;
	
//	*****************************************************************************
	
	/** Creates new GridCommand */
	public AnimationStartStepCommand(JInternalFrame parent) {
		this.parent = (InterfacePostp)parent;
	}
	
	/** Creates new GridCommand */
	public AnimationStartStepCommand() {
		
	}
	
//	*****************************************************************************
	
	public synchronized void execute() {
		if(((PostpViewState)this.parent.getView().getViewState()).isDeformedOn()){
			Start start = new Start(this.parent);
			thread = new Thread(start);
			thread.start();
		}else{
			String message = "You must set Deformed shape!";
			JOptionPane.showMessageDialog(this.parent.getDesktop(), message, "Error Message", JOptionPane.ERROR_MESSAGE);
		}
	}
	
//	*****************************************************************************
	
	public void undo() {
	}
	
//	*****************************************************************************
	
	public void redo() {
	}
	
	public InterfacePostp getParent() {
		return parent;
	}
	
	public void setParent(InterfacePostp parent) {
		this.parent = parent;
	}
	
//	*****************************************************************************
}

class Start implements Runnable{
	
	InterfacePostp parent;
	PostpModel postpModel = new PostpModel();
	StepPostp stepPostp = new StepPostp();
	DecimalFormat fmt = new DecimalFormat();
	ArrayList steps;
	StepPostp current;
	
	public Start(InterfacePostp parent){
		this.parent = parent;
	}
	
	public synchronized void run() {
		int index = 0;
		int cont =0;
		
		if(((PostpViewState)parent.getView().getViewState()).getLoadViewState().isOnMemoryOpt()){
			((PostpViewState)parent.getView().getViewState()).getStepState().setAnimationActiveted(true);
			while(((PostpViewState)parent.getView().getViewState()).getStepState().isAnimationActiveted()){
				current = ((StepPostp)this.parent.getModel()); 
				index = this.parent.getPostpModel().getStepList().indexOf(current);
				cont = (int)this.parent.getDesktop().getCurrentStepField().getValue();
				while(index<this.parent.getPostpModel().getStepList().size()-1){
					if(((PostpViewState)parent.getView().getViewState()).getStepState().isAnimationActiveted()){
						
						this.parent.getDesktop().getCurrentStepField().setValue(cont);
						this.parent.setStep((StepPostp)(this.parent.getPostpModel().getStepList().get(index)));
						try{
							Thread.sleep(100);
						}catch (Exception e){
							
						}
						index++;
						cont++;
					}else{
						this.parent.getDesktop().getCurrentStepField().setValue(1);
						this.parent.setStep((StepPostp)(this.parent.getPostpModel().getStepList().get(0)));
						break;
					}
				}
				this.parent.getDesktop().getCurrentStepField().setValue(this.parent.getPostpModel().getStepList().size());
				this.parent.setStep((StepPostp)(this.parent.getPostpModel().getStepList().get(this.parent.getPostpModel().getStepList().size()-1)));
				current = ((StepPostp)this.parent.getModel()); 
				index = this.parent.getPostpModel().getStepList().indexOf(current);
				cont = (int)this.parent.getDesktop().getCurrentStepField().getValue();
				while(index!=0){
					if(((PostpViewState)parent.getView().getViewState()).getStepState().isAnimationActiveted()){
						this.parent.getDesktop().getCurrentStepField().setValue(cont);
						this.parent.setStep((StepPostp)(this.parent.getPostpModel().getStepList().get(index)));
						try{
							Thread.sleep(100);
						}catch (Exception e){
							
						}
						index--;
						cont--;
					}else{
						this.parent.getDesktop().getCurrentStepField().setValue(1);
						this.parent.setStep((StepPostp)(this.parent.getPostpModel().getStepList().get(0)));
						break;
					}
				}
				this.parent.getDesktop().getCurrentStepField().setValue(1);
				this.parent.setStep((StepPostp)(this.parent.getPostpModel().getStepList().get(0)));
			}
		}else if(((PostpViewState)parent.getView().getViewState()).getLoadViewState().isOnDiskOpt()){
			((PostpViewState)parent.getView().getViewState()).getStepState().setAnimationActiveted(true);
			while(((PostpViewState)parent.getView().getViewState()).getStepState().isAnimationActiveted()){
				
				steps =((PostpViewState)this.parent.getView().getViewState()).getLoadViewState().getStepLoaded();
				PostpPersistenceAsXML p = new PostpPersistenceAsXML();
				int currentStep = (int)this.parent.getDesktop().getCurrentStepField().getValue();
				
				while(currentStep<steps.size()){
					if(((PostpViewState)parent.getView().getViewState()).getStepState().isAnimationActiveted()){
						p = new PostpPersistenceAsXML();
						p.fillInsaneFromFile((String)steps.get(currentStep-1));
						stepPostp = new StepPostp();
						stepPostp = p.getStep();
						postpModel = new PostpModel();
						postpModel.addStep(stepPostp);
						parent.setStep((StepPostp)postpModel.getStepList().get(0));
						parent.setPostpModel(postpModel);
						currentStep++;
						try{
							Thread.sleep(100);
						}catch (Exception e){
							
						}
					}else{
						currentStep=1;
						p = new PostpPersistenceAsXML();
						p.fillInsaneFromFile((String)steps.get(currentStep-1));
						stepPostp = new StepPostp();
						stepPostp = p.getStep();
						postpModel = new PostpModel();
						postpModel.addStep(stepPostp);
						parent.setStep((StepPostp)postpModel.getStepList().get(0));
						parent.setPostpModel(postpModel);
						this.parent.getDesktop().getCurrentStepField().setValue(currentStep);
						break;
					}
				}
				
				currentStep = steps.size();
				this.parent.getDesktop().getCurrentStepField().setValue(currentStep);
				p = new PostpPersistenceAsXML();
				p.fillInsaneFromFile((String)steps.get(currentStep-1));
				stepPostp = new StepPostp();
				stepPostp = p.getStep();
				postpModel = new PostpModel();
				postpModel.addStep(stepPostp);
				parent.setStep((StepPostp)postpModel.getStepList().get(0));
				parent.setPostpModel(postpModel);
				currentStep--;
				
				while(currentStep!=0){
					if(((PostpViewState)parent.getView().getViewState()).getStepState().isAnimationActiveted()){
						p = new PostpPersistenceAsXML();
						p.fillInsaneFromFile((String)steps.get(currentStep-1));
						stepPostp = new StepPostp();
						stepPostp = p.getStep();
						postpModel = new PostpModel();
						postpModel.addStep(stepPostp);
						parent.setStep((StepPostp)postpModel.getStepList().get(0));
						parent.setPostpModel(postpModel);
						this.parent.getDesktop().getCurrentStepField().setValue(currentStep);
						currentStep--;
						try{
							Thread.sleep(100);
						}catch (Exception e){
							
						}
					}else{
						currentStep=1;
						p = new PostpPersistenceAsXML();
						p.fillInsaneFromFile((String)steps.get(currentStep-1));
						stepPostp = new StepPostp();
						stepPostp = p.getStep();
						postpModel = new PostpModel();
						postpModel.addStep(stepPostp);
						parent.setStep((StepPostp)postpModel.getStepList().get(0));
						parent.setPostpModel(postpModel);
						this.parent.getDesktop().getCurrentStepField().setValue(currentStep);
						break;
					}
					
				}
				currentStep=1;
				p = new PostpPersistenceAsXML();
				p.fillInsaneFromFile((String)steps.get(currentStep-1));
				stepPostp = new StepPostp();
				stepPostp = p.getStep();
				postpModel = new PostpModel();
				postpModel.addStep(stepPostp);
				parent.setStep((StepPostp)postpModel.getStepList().get(0));
				parent.setPostpModel(postpModel);
				this.parent.getDesktop().getCurrentStepField().setValue(currentStep);
			}
			
		}
		
	}
	
}

